const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
// hex转ArrayBuffer
function strToBuf(arr) {
  let length = arr.length
  let buffer = new ArrayBuffer(length + 2)
  let dataview = new DataView(buffer)
  for (let i = 0; i < length; i++) {
    dataview.setUint8(i, '0x' + arr[i])
  }
  return buffer
}
// ArrayBuffer转16进度字符串示例
function ab2hex(buffer) {
  let hexArr = Array.prototype.map.call(
    new Uint8Array(buffer),
    function (bit) {
      return ("00" + bit.toString(16)).slice(-2);
    }
  );
  return hexArr.join("");
}

export default {
  formatTime: formatTime,
  strToBuf,
  ab2hex
}